package com.example.demo3.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo3.domain.Permission;
import com.example.demo3.domain.bo.PermissionBo;
import com.example.demo3.domain.vo.PermissionVo;
import com.example.demo3.mapper.PermissionsMapper;
import com.example.demo3.utils.BeanConvertUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PermissionsService   {

    private final PermissionsMapper permissionsMapper ;


    public List<PermissionVo> queryPermissionList(PermissionBo permissionBo) {
        QueryWrapper queryWrapper = new QueryWrapper<Menu>();
//        queryWrapper.eq("permission", permissionBo.getPermission());
//        queryWrapper.like("permission_name", permissionBo.getPermissionName());
        return permissionsMapper.selectList(queryWrapper);
    }

    @Transactional
    public Boolean insertByBo(PermissionBo permissionBo) {
        int i = 0  ;
        try{
            Permission permissions = BeanConvertUtils.convertVOtoBO(permissionBo, Permission.class);
             i = permissionsMapper.insert(permissions);
            System.out.println(2/0);
        }catch (Exception e ){
            // 事务测试 ，抛异常 触发事务
            System.out.println(e.getMessage());
            throw  new IndexOutOfBoundsException() ;
        }
        return i > 0  ;
    }

    public Boolean updateByBo(PermissionBo permissionBo) {
        Permission permissions = BeanConvertUtils.convertVOtoBO(permissionBo, Permission.class);
        permissions.getUpdate();
        return permissionsMapper.updateById(permissions) > 0;
    }

    public Boolean deleteById(String[] ids) {
        List<String> idList = Arrays.stream(ids).toList();
        return permissionsMapper.deleteBatchIds(idList) > 0 ;
    }
}
